'use strict';

angular.module('users').factory('Profile', ['$http','$q','localStorageService',
	function($http,$q,localStorageService) {
		// Profile service logic
		// ...

		// Public API
		return {
			suggest: function(val) {
				return $http.get('/users', {
					params: {
						search: val
					}
				}).then(function(response){
						return response.data
							.map(function(item){
							return {id:item._id,displayName:item.displayName};
						});
				});
			},
			allGroups: function() {
				return $http.get('/groups/all').then(function(response){
					return response.data;
				});
			},
			getUsersOfGroup: function(groupId) {
				var deferred = $q.defer(),
					promise = deferred.promise;

				var cachedData = localStorageService.get(groupId);
				if(angular.isObject(cachedData)){
					setTimeout(deferred.resolve(cachedData),100);
				}else{
					$http.get('/users/bygroup', {
						params: {
							group: groupId
						}
					}).then(function(response){
						localStorageService.set(groupId,response.data);
						deferred.resolve(response.data);
						/*.map(function(item){
						 return {id:item._id,displayName:item.displayName};
						 });*/
					});
				}
				return promise;
			},
			getUsersForIds:function(userIds){
				var deferred = $q.defer(),
					promise = deferred.promise;
				var users={};
				var usersToFetch=[];
				var cachedData = localStorageService.get('Users');

				if(angular.isObject(cachedData)) {
					// if request is for single user id
					if(angular.isString(userIds)){
						// if user id is there in cached data;
						if(angular.isDefined(cachedData[userIds])){
								users[userIds]=cachedData[userIds];
						}else{
							usersToFetch.push(userIds);
						}
					}else if(angular.isArray(userIds)){
						for(var idx=0;idx<userIds.length;idx++){
							var uId = userIds[idx];
							// if user id is there in cached data;
							if(angular.isDefined(cachedData[uId])){
									users[uId]=cachedData[uId];
							}else{
								usersToFetch.push(uId);
							}
						}
					}
				}else{
					cachedData={};
					usersToFetch = userIds;
				}

				if(usersToFetch.length>0){
					$http.post('/users/getByIds', {
						params: {
							userIds: usersToFetch
						}
					}).then(function(response){
						var fetchedUsers = response.data,usr;
						for(var idx=0;idx<fetchedUsers.length;idx++){
							usr=fetchedUsers[idx];
							cachedData[usr._id] = usr.displayName;
							users[usr._id]=usr.displayName;
						}
						localStorageService.set('Users',cachedData);
						deferred.resolve(users);
					});
				}else{
					setTimeout(deferred.resolve(users),100);
				}
				return promise;
			},
			populateUsersDisplayName:function(arrayObj,userIdProperty){
				var deferred = $q.defer(),
					promise = deferred.promise;
				var arrayOfUserIds = arrayObj.map(function(item){return item[userIdProperty];});
				this.getUsersForIds(arrayOfUserIds).then(function(users){
					arrayObj.forEach(function(user){
						user.displayName = users[user[userIdProperty]];
					});
					deferred.resolve(arrayObj);
				});
				return promise;

			}
		};
	}
]);
